<?php
include_once('../variables.php');
$_SESSION['lang'] = $lang;
if (!isset($_SESSION['lang']) || eregi("^\w{2}$", $_SESSION['lang']))
  $_SESSION['lang'] = $defaultLang;

$langFile = "languages/" . $_SESSION['lang'] . "/" . $fileTemplate;
if (file_exists($langFile) && is_file($langFile)) {
  include($langFile);
}
//hiena: spiram reportvaneto na problemi zashtoto qvno ne sa napraveni vsichki filove i vadi grozni greshki
//else
//  reportProblem('File not exists: ' . $langFile);
include('languages/' . $_SESSION['lang'] . '/main.html');
include('languages/' . $_SESSION['lang'] . '/errorMessages.html');

function translate_error($errora) {
    $errorMsg = translate($errora);
    include('errorMsg.html');
}    
    
function reportProblem($string) {
   echo('problem: ' . $string);
}
function silent_report_problem($file, $string, $english) {
   $fp = fopen("/tmp/langs.errs", "a+");
   fputs($fp, "Missing string $string ($english) in file: $file, lang: " . $_SESSION['lang'] . "\n");
   fclose($fp);
}

//params ima format array('param1'=>'value1', 'param2'=>'value2',...)
//i ako e setnat zamestva paramX ot prevoda s valueX
function translate($string, $params = array()) {
     global $_LANG, $fileTemplate;

    //hiena: puskam go ako nqma stringa da vrashta podadeniq
    //stratsimir: kato niama string pishe vuv file lipsvashtia string
    // if ($_SERVER['REMOTE_ADDR'] == '213.222.51.226')
     //	$flag = 1;
     if(!$res = $_LANG[$string]) {
       	 include("languages/en/$fileTemplate");
         silent_report_problem($fileTemplate, $string, $_LANG[$string]);
         if ($_LANG[$string] != "") $res = stripslashes($_LANG[$string]);
         else $res = $string;
     }
     foreach ($params as $param=>$value) {         
         $res = ereg_replace("%$param%", $value, $res);
     }
     
     if ($_LANG[$string] == '') { silent_report_problem($fileTemplate, $string, $string); return $string; } 
     return stripslashes($res);
}
?>
